#include "mainwindow.h"
#include "ui_mainwindow.h"




MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  //初始化变量
  ui->setupUi(this);
  Init();
}

void MainWindow::Init()
{
    cmenu=NULL;
    add_dtv=NULL;
    add_atv=NULL;
    tcl_usb=new TCL_USB();
    sortFlag=true;
    ch_is_exist=false;
    is_atv_edit=false;
    is_dtv_edit=false;
 //   lastpage=true;
    //md_list_row=0;
    count_col_atv=0;
    count_col_dtv=0;
    count_row_atv=0;
    count_row_dtv=0;
    cur_show=-1;
    cur_row=-1;
    read_file_name=STD_FILE_FR_TV;
    write_file_name=STD_FILE_FR_PC;
    model_atv=new QStandardItemModel;
    model_dtv=new QStandardItemModel;
    ms_tv=new MSTAR_TV;
    mt_tv=new MTK_TV;
    Chan_atv_Num=new QList<uint>;
    Chan_dtv_air_Num=new QMap<uint,uchar>;
    Chan_dtv_cable_Num=new QMap<uint,uchar>;
    cur_atv_loc=0;
    cur_dtv_loc=0;
    next_atv_loc=0;
    next_dtv_loc=0;
    md_ms_atv=NULL;
    md_mt_atv=NULL;
    md_mt_dtv=NULL;
    md_ms_dtv=NULL;
    Search_Show=NULL;
    chinese=NULL;
    english=NULL;
    memset(disk,0,FILE_PATH_LEN);
    Menu_Open();
    //界面控件初始化
   // ui->an_DelCol->setVisible(false);
   // ui->an_DelRow->setVisible(false);
    //ui->an_save_ATV->setVisible(false);
   // ui->an_ShowDTV->setVisible(false);
    //ui->actionConvert_standard_file->setEnabled(true);
    ui->actionConvert_tv_file->setEnabled(false);
    ui->actionOpen_standard_file->setEnabled(false);
    ui->actionWrite_stadard_file->setEnabled(false);

     ui->actionOpen_standard_file->setVisible(false);
     ui->actionWrite_stadard_file->setVisible(false);
     ui->actionSave_as->setVisible(false);

    ui->apB_LastPage->setEnabled(false);
    ui->apB_NextPage->setEnabled(false);
    ui->dpB_LastPage->setEnabled(false);
    ui->dpB_NextPage->setEnabled(false);

    ui->tableView_atv->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView_dtv->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView_atv->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView_dtv->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->tabWidget->setEnabled(false);
    //最后一列全部填充View
    ui->tableView_atv->horizontalHeader()->setStretchLastSection(true);
    ui->tableView_dtv->horizontalHeader()->setStretchLastSection(true);
    ui->tabWidget->setEnabled(false);
    ui->anDel_Row->setEnabled(false);
    ui->actionShow->setEnabled(false);
    ui->actionSearch->setEnabled(false);
    ui->actionModify_Ctrl_M->setEnabled(false);
    //列表头属性
   // ui->tableView_atv->horizontalHeader()->setStyleSheet("background-color: rgb(135, 206, 235)");
    //ui->tableView_dtv->horizontalHeader()->setStyleSheet("backround-color: rgb(135,206,235");
    ui->centralWidget->setStyleSheet("background-color:rgb(135,206,235)");
    ui->apB_LastPage->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->apB_NextPage->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->dpB_LastPage->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->dpB_NextPage->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");


    connect(ui->actionSearch,SIGNAL(triggered()),this,SLOT(Sea_Row()));
    ui->actionSearch->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_F));
    connect(ui->actionShow,SIGNAL(triggered()),this,SLOT(Show_ATV()));
    connect(ui->actionShow,SIGNAL(triggered()),this,SLOT(Show_DTV()));
    ui->actionShow->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_S));
    connect(ui->anDel_Row,SIGNAL(triggered()),this,SLOT(Del_Row()));
    ui->anDel_Row->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_D));
    connect(ui->actionModify_Ctrl_M,SIGNAL(triggered()),this,SLOT(Md_Data()));
    ui->actionModify_Ctrl_M->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_M));
    ui->actionEnglish->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_H));
    ui->actionConvert_tv_file->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_S));
   // ui->actionConvert_standard_file->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_O));

    //Open std file before data edit.
     connect(ui->actionOpen_standard_file,SIGNAL(triggered()),this,SLOT(open_file()));
    //write std file after datas edited .
    connect(ui->actionWrite_stadard_file,SIGNAL(triggered()),this,SLOT(Write_File()));
    //clicked tableview header sort
      connect(ui->tableView_atv->horizontalHeader(),SIGNAL(sectionPressed(int)),this,SLOT(sortSlot(int)));
      connect(ui->tableView_dtv->horizontalHeader(),SIGNAL(sectionPressed(int)),this,SLOT(sortSlot(int)));


     // connect(ui->actionConvert_standard_file,SIGNAL(triggered()),this,SLOT(Menu_Open()));
      connect(ui->an_open_USB,SIGNAL(triggered()),this,SLOT(Open_USB()));
      connect(ui->an_openPC,SIGNAL(triggered()),this,SLOT(Open_PC()));
      connect(ui->actionConvert_tv_file,SIGNAL(triggered()),this,SLOT(Con_tv_file()));

      connect(ui->tableView_atv,SIGNAL(clicked(QModelIndex)),this,SLOT(set_cur_row(QModelIndex)),Qt::DirectConnection);
      connect(ui->tableView_dtv,SIGNAL(clicked(QModelIndex)),this,SLOT(set_cur_row(QModelIndex)),Qt::DirectConnection);

      ui->tableView_atv->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(ui->tableView_atv,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(Show_Menu(QPoint)),Qt::DirectConnection);

      ui->tableView_dtv->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(ui->tableView_dtv,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(Show_Menu(QPoint)),Qt::DirectConnection);

      connect(ui->apB_NextPage,SIGNAL(clicked()),this,SLOT(NextPage()));
      connect(ui->apB_LastPage,SIGNAL(clicked()),this,SLOT(LastPage()));

      connect(ui->dpB_NextPage,SIGNAL(clicked()),this,SLOT(NextPage()));
      connect(ui->dpB_LastPage,SIGNAL(clicked()),this,SLOT(LastPage()));


      connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(set_cur_show(int)));
      connect(ui->actionChinexe,SIGNAL(triggered()),this,SLOT(Show_Ch_Help()));
      connect(ui->actionEnglish,SIGNAL(triggered()),this,SLOT(Show_En_Help()));
}

void MainWindow::SetTableHeader(ushort flag)
{
    if(flag==0)//atv
    {      //atv
        if(!head_data.isEmpty())
        head_data.clear();
        head_data.append("ch_val");
        head_data.append("name");
        head_data.append("class");
        head_data.append("freqency");
        head_data.append("favorite");
        head_data.append("skip");
        model_atv->setColumnCount(head_data.size());
        count_col_atv=head_data.size();
        for(int i=0;i<head_data.size();i++)
        {
            model_atv->setHeaderData(i,Qt::Horizontal,head_data.at(i));
        }
    }
     else
    {
        head_data.clear();
        head_data.append("ch_val");
        head_data.append("name");
        head_data.append("class");
        head_data.append("type");
        head_data.append("frequency");
        head_data.append("favorite");
        head_data.append("skip");
        Q_CHECK_PTR(model_dtv);
        model_dtv->setColumnCount(head_data.size());
        count_col_dtv=head_data.size();
        for(int i=0;i<head_data.size();i++)
            model_dtv->setHeaderData(i,Qt::Horizontal,head_data.at(i));
    }
}
void MainWindow::Refresh()
{
    model_atv->clear();
    model_dtv->clear();

    tcl_usb->Clear();
    ms_tv->Clear();

}
//this program need atv tvs don't have the same channel number.so as dtv
int MainWindow::Find_List_Row()
{
    uint atv_num=0;
    uint dtv_num=0;
    bool Flag=false;
    bool *ok=&Flag;
  switch(cur_show)
  {
  case 0://mtk_atv
      atv_num=model_atv->index(cur_row,0).data(0).toUInt(ok);
      Q_ASSERT(*ok==true);
      for(uint i=0;i<mt_tv->Get_ATV_Count();i++)
      {
          if(atv_num==mt_tv->Get_ATV_Num(i))
              return i;
      }
    break;

  case 1://mtk_dtv
      dtv_num=model_dtv->index(cur_row,0).data(0).toUInt(ok);
      Q_ASSERT(*ok==true);
      for(uint i=0;i<mt_tv->Get_DTV_Count();i++)
      {
          if(dtv_num==mt_tv->Get_DTV_Num(i))
          {
              return i;
          }
      }
    break;

  case 2://mstar_atv
  {
      Q_CHECK_PTR(model_atv);
      atv_num=model_atv->index(cur_row,0).data(0).toUInt();
     /* for(ushort i=0;i<256;i++)
      {
          if(atv_num==ms_tv->Get_ATV_Num(i))
              return  i;
      }*/
      return atv_num-1;
      Q_CHECK_PTR(ms_tv);
      //Q_ASSERT(ms_tv->mstar_atv!=NULL);
     // qDebug("In MainWindow::Find_List_Row look for mstar_atv list num  Error\n");
      break;
  }
  case 3:
  {
      Q_CHECK_PTR(model_dtv);
      dtv_num=model_dtv->index(cur_row,0).data(0).toUInt();
      ushort dtv_type=0;
      ushort dtv_class=0;
      QString tmp=model_dtv->index(cur_row,2).data(0).toString();
      QString btmp="DTV-B";
      QString ctmp="DTV-C";
      if(0==tmp.compare(btmp))
          dtv_class=0;
      else if(0==tmp.compare(ctmp))
          dtv_class=1;
      else
          dtv_class=1;
      tmp=model_dtv->index(cur_row,3).data(0).toString();
      QString dtmp="T";
      QString etmp="U";
      btmp="D";
      ctmp="R";
      if(tmp.compare(btmp)==0)
      {
             dtv_type=1;
    /*          if(dtv_class==1)
              {
                  start=ms_tv->Get_DTV_Sum(1);
                  end=start+ms_tv->dtvb_type.get_item(0);
              }else
              {
                  start=0;
                  end=ms_tv->dtvb_type.get_item(1);
              }
              */
      }
      else if(tmp.compare(ctmp)==0)
      {
          dtv_type=2;
        /*  if(dtv_class==1)
          {
              start=ms_tv->Get_DTV_Sum(1)+ms_tv->dtvb_type.get_item(0);
              end=start+ms_tv->dtvb_type.get_item(1);
          }else
          {
              start=ms_tv->dtvb_type.get_item(0);
              end=start+ms_tv->dtvb_type.get_item(1);
          }*/
      }
      else if(tmp.compare(dtmp)==0)
      {
              dtv_type=3;
           /*   if(dtv_class==1)
              {
                  start=ms_tv->Get_DTV_Sum(1)+ms_tv->dtvb_type.get_item(0)+ms_tv->dtvb_type.get_item(1);
                  end=start+ms_tv->dtvb_type.get_item(2);
              }else
              {
                  start=ms_tv->dtvb_type.get_item(0)+ms_tv->dtvb_type.get_item(1);
                  end=start+ms_tv->dtvb_type.get_item(2);
              }*/
     }else if(tmp.compare(etmp)==0)
      {
          dtv_type=4;
       /*   if(dtv_class==1)
          {
            start=ms_tv->Get_DTV_Sum(1)+ms_tv->dtvb_type.get_item(0)+ms_tv->dtvb_type.get_item(1)+ms_tv->dtvb_type.get_item(2);
            end=start+ms_tv->dtvb_type.get_item(3);
          }else{
              start=ms_tv->dtvb_type.get_item(0)+ms_tv->dtvb_type.get_item(1)+ms_tv->dtvb_type.get_item(2);
              end=start+ms_tv->dtvb_type.get_item(3);
          }*/
      }

      Q_CHECK_PTR(ms_tv);
      for(ushort i=cur_dtv_loc;i<tcl_usb->q_mstar_dtv_tv.size()&&(i-cur_dtv_loc)<MAX_LISTPAGE_SIZE;i++)
      {
//在修改界面出来后，tcl_usb已经改变，无法找到修改前选择的那一列，所以使用ms_tv中的数据
           ushort num=ms_tv->mstar_dtv.at(i)->ui2_Number;
                  //tcl_usb->q_mstar_dtv_tv.at(i)->Get_Item(0).toUShort(ok);
          ushort type=ms_tv->mstar_dtv.at(i)->u_ServiceType;
                  //tcl_usb->q_mstar_dtv_tv.at(i)->Get_Item(3).toUShort(ok);
          ushort _class=tcl_usb->q_mstar_dtv_tv.at(i)->Get_Item(2).toUShort(ok);//class不能修改所以不变
          if(dtv_num==num&&
                  dtv_type==type&&
                  dtv_class==_class)
          {
              Q_ASSERT(*ok==true);
              return i;
          }
          //Q_ASSERT(*ok==true);
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_dtv list num Error\n");
      break;
  }
  default:
      qDebug("In MainWindow::Find_List_Row cur_show error");

  }
  return 0;
}
/*
int MainWindow::Find_List_Row(int row)
{
  switch(cur_show)
  {
  case 0://mtk_atv

  case 1://mtk_dtv

  case 2://mstar_atv
  {
      Q_ASSERT(model_atv!=NULL);
      int atv_num=model_atv->index(row,0).data(0).toUInt();
      Q_ASSERT(ms_tv->mstar_atv!=NULL);
      for(int i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
      {

          if(atv_num==(int)ms_tv->mstar_atv->ATVProgramData[i].misc.u_ChannelNumber)
              return i;
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_atv list num  Error\n");
      break;
  }
  case 3:
  {
      Q_ASSERT(model_dtv!=NULL);
      int dtv_num=model_dtv->index(row,0).data(0).toUInt();
      Q_ASSERT(ms_tv!=NULL);
      for(uint i=0;i<ms_tv->Get_DTV_Count();i++)
      {

          if(dtv_num==(int)ms_tv->mstar_dtv.at(i)->ui2_Number)
              return i;
      }
      qDebug("In MainWindow::Find_List_Row look for mstar_dtv list num Error\n");
      break;
  }
  default:
      qDebug("In MainWindow::Find_List_Row cur_show error");

  }
  return -1;
}
*/
MainWindow::~MainWindow()
{
    delete ui;
    Clear();

}


 void MainWindow::Clear()
{
    if(tcl_usb)
    delete tcl_usb;
    tcl_usb=NULL;
    if(model_atv)
    delete model_atv;
    if(model_dtv)
    delete model_dtv;
    model_atv=NULL;
    model_dtv=NULL;
    if(ms_tv)
    delete ms_tv;
    ms_tv=NULL;
    /* if(!cmenu->isEmpty())
        cmenu->clear();
    if(cmenu)
    delete cmenu;
    cmenu=NULL;
    if(mt_tv)
    delete mt_tv;
    mt_tv=NULL;*/
    if(!Chan_atv_Num->isEmpty())
        Chan_atv_Num->clear();
    if(Chan_atv_Num)
    delete Chan_atv_Num;

    Chan_atv_Num=NULL;

    if(!Chan_dtv_air_Num->isEmpty())
        Chan_dtv_air_Num->clear();
    if(Chan_dtv_air_Num)
    delete Chan_dtv_air_Num;
    Chan_dtv_air_Num=NULL;

    if(!Chan_dtv_cable_Num->isEmpty())
        Chan_dtv_cable_Num->clear();
    if(Chan_dtv_cable_Num)
    delete Chan_dtv_cable_Num;
    Chan_dtv_cable_Num=NULL;

    if(!last_atv_loc.isEmpty())
    last_atv_loc.clear();
    if(!last_dtv_loc.isEmpty())
    last_dtv_loc.clear();
    if(md_ms_atv)
        delete md_ms_atv;
    if(md_mt_dtv)
        delete md_mt_atv;
    if(md_ms_dtv)
        delete md_ms_dtv;
    if(md_mt_atv)
        delete md_mt_dtv;
    md_ms_atv=NULL;
    md_mt_atv=NULL;
    md_mt_dtv=NULL;
    md_ms_dtv=NULL;
    if(Search_Show)
        delete Search_Show;
    Search_Show=NULL;
    if(chinese)
        delete chinese;
    chinese=NULL;
    if(english)
        delete english;
    english=NULL;

}
void MainWindow::sortSlot(int column)
{

  if(sortFlag)
  {
      if(0==cur_show||2==cur_show)
      {
        Q_ASSERT(ui->tableView_atv!=NULL);
        ui->tableView_atv->sortByColumn(column,Qt::AscendingOrder);
      }
      else if(1==cur_show||3==cur_show)
      {
          Q_ASSERT(ui->tableView_dtv!=NULL);
          ui->tableView_dtv->sortByColumn(column,Qt::AscendingOrder);
      }
      else
      {
          qDebug("In MainWindow::sortSlot cur_show error\n");
          return ;
      }
      sortFlag=false;
  }
  else
  {
      if(0==cur_show||2==cur_show)
      {
        Q_ASSERT(ui->tableView_atv!=NULL);
        ui->tableView_atv->sortByColumn(column,Qt::AscendingOrder);
      }
      else if(1==cur_show||3==cur_show)
      {
          Q_ASSERT(ui->tableView_dtv!=NULL);
          ui->tableView_dtv->sortByColumn(column,Qt::AscendingOrder);
      }
      else
      {
          qDebug("In MainWindow::sortSlot cur_show error\n");
          return ;
      }
      sortFlag=true;
  }
  if(0==cur_show||2==cur_show)
  {
      for(int i=0;i<count_row_atv;i++)
      {
          for(int j=0;j<count_col_atv;j++)
          {
               model_atv->item(i,j)->setTextAlignment(Qt::AlignCenter);
          // if(i%2==0)
          //     model_atv->item(i,j)->setBackground(QBrush(QColor(0,191,255)));
          // else
             model_atv->item(i,j)->setBackground(QBrush(QColor(135,206,235)));
          }
      }

      for(int i=0;i<count_row_dtv;i++)
          for(int j=0;j<count_col_dtv;j++)
          {
            model_dtv->item(i,j)->setTextAlignment(Qt::AlignCenter);
          // if(i%2==0)
         //  model_dtv->item(i,j)->setBackground(QBrush(QColor(0,191,255)));
           //else
             model_dtv->item(i,j)->setBackground(QBrush(QColor(135,206,235)));
          }

  }
}

void MainWindow::Read_File()
{
  qDebug("Read File in MainWindows");
  if(!tcl_usb->is_empty())
  {
    //  cmenu=NULL;
      delete tcl_usb;
      tcl_usb=NULL;
      sortFlag=true;
      ch_is_exist=false;
      is_atv_edit=false;
      is_dtv_edit=false;
      count_col_atv=0;
      count_col_dtv=0;
      cur_show=-1;
      cur_row=-1;
      cur_atv_loc=0;
      cur_dtv_loc=0;
      next_atv_loc=0;
      next_dtv_loc=0;
      last_atv_loc.clear();
      last_dtv_loc.clear();

      //Clear();
  }
  last_atv_page=true;
  last_dtv_page=true;
  //last_dtv_loc.push_back(0);
  //if(!tcl_usb)
  tcl_usb=new TCL_USB(read_file_name);
  QFile file(read_file_name);
 // string str_file_name=file_name.toStdString();
  if(!file.open(QFile::ReadOnly))
  {
      qDebug("Open File bin Fialed\n");
      return ;
  }
  QDataStream in(&file);
  Q_ASSERT(tcl_usb!=NULL);
  if(!tcl_usb->is_empty())
  {
      tcl_usb->Clear();
  }

  ch_is_exist=tcl_usb->Read_File(in,read_file_name);


  if(solution==1)
       Set_MS_ATV_Num();//计算MS ATV的频道号
  //The Default:The Tools Show ATV Data.
  if(0==tcl_usb->Get_Solution())
  {
      cur_show=0;
  }else if(1==tcl_usb->Get_Solution())
      cur_show=2;
  else
      qDebug("In MainWindow::Read_File Get_sulotion errorn");

  qDebug("fuck");
  ui->apB_LastPage->setEnabled(true);
  ui->apB_NextPage->setEnabled(true);
  ui->dpB_LastPage->setEnabled(true);
  ui->dpB_NextPage->setEnabled(true);

  if(model_atv)
  {
    model_atv->clear();
  }

  if(model_dtv)
  {
   model_dtv->clear();
  }


  Show_ATV();
  Show_DTV();
  ui->actionWrite_stadard_file->setEnabled(true);
  ui->tabWidget->setEnabled(true );
}

//写文件前更新记录表格数据的内容
void MainWindow::Write_File()
{
  qDebug("Write File in MainWindow\n");

  QFile file(write_file_name);
  if(!file.open(QFile::WriteOnly))
  {
      qDebug("Open File form_pc_std.bin Failed\n");
      return ;
  }
  QDataStream out(&file);
  Q_ASSERT(tcl_usb!=NULL);
  tcl_usb->Write_File(out);
  ui->actionWrite_stadard_file->setEnabled(true);
}


void MainWindow::open_file()
{
  /*read_file_name = QFileDialog::getOpenFileName(
     this,
     "Open Document",
     QDir::currentPath(),
     "Document files (*.bin);;All files(*.*)");*/
    read_file_name=STD_FILE_FR_TV ;
  if (!read_file_name.isNull()) { //用户选择了文件
     // 处理文件
     Read_File();
  } else // 用户取消选择
     QMessageBox::information(this, "Document", "No document", QMessageBox::Ok | QMessageBox::Cancel);
  ui->actionConvert_tv_file->setEnabled(true);
}


  QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MSTAR_ATV_TV* mstar_atv_tv)
{
    Q_CHECK_PTR(mstar_atv_tv!=NULL);
    QList<QStandardItem *> ItemRow;
    QString tmp;
    QList<QString> tmp_str;
    mstar_atv_tv->Get_Data(&tmp_str);
    for(int j=0;j<tmp_str.size();j++)
    {
        tmp=tmp_str.at(j);
      ItemRow<<(new QStandardItem(tmp));
    }
    tmp_str.clear();
    return ItemRow;
}

   QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MTK_ATV_TV* mtk_atv_tv)
{
    Q_CHECK_PTR(mtk_atv_tv);
    QList<QString> tmp_str;
    QList<QStandardItem *>ItemRow;
    mtk_atv_tv->Get_Data(&tmp_str);
    QString tmp;
    for(int j=0;j<tmp_str.size();j++)
    {
         tmp=tmp_str.at(j);
         ItemRow<<(new QStandardItem(tmp));
    }
    return ItemRow;

}

   QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MSTAR_DTV_TV *mstar_dtv_tv)
{
    Q_CHECK_PTR(mstar_dtv_tv);
    QList<QString> tmp_str;
    QList<QStandardItem *> ItemRow;
    mstar_dtv_tv->Get_Data(&tmp_str);
    for(int j=0;j<tmp_str.size();j++)
    {
        ItemRow<<(new QStandardItem(tmp_str.at(j)));
    }
    tmp_str.clear();
    return ItemRow;
}

   QList<QStandardItem *> MainWindow::Get_Item_Row(TCL_MTK_DTV_TV *mtk_dtv_tv)
{

    Q_CHECK_PTR(mtk_dtv_tv);
    QList<QString>tmp_str;
    QString tmp;
    QList<QStandardItem *> ItemRow;
    mtk_dtv_tv->Get_Data(&tmp_str);

    for(int j=0;j<tmp_str.size();j++)
    {
        tmp=tmp_str.at(j);
        ItemRow<<(new QStandardItem(tmp));
     }
    tmp_str.clear();
    return ItemRow;
}

    void MainWindow::Show_ATV()
{
      model_atv->clear();
      SetTableHeader(0);
      QList<QStandardItem*> ItemRow;
      QString Solution;
    //  QString TextShow="当前显示ATV信息，请点击单元格直接修改内容,修改完毕后请点击编辑->保存ATV,否则当前修改将丢失";
      modify=false;//
      Q_CHECK_PTR(tcl_usb);
      count_row_atv=0;
      if(tcl_usb->q_mtk_atv_tv.size()!=0)
      {
          Solution=QString("MTK_ATV");
          TCL_MTK_ATV_TV *mtk_atv_tv;
          count_row_atv=0;//记录行数
          int i=0;
          int tmp=cur_atv_loc;
          if(last_atv_page)
          {
             last_atv_loc.push_back(tmp);
             last_atv_page=false;
          }
          for(i=cur_atv_loc;i<tcl_usb->q_mtk_atv_tv.size()&&count_row_atv<SHOW_NUM_DATA;i++)
          {
                      mtk_atv_tv=tcl_usb->q_mtk_atv_tv.at(i);
                      Q_CHECK_PTR(mtk_atv_tv);
                      ItemRow=Get_Item_Row(mtk_atv_tv);
                      count_row_atv++;
                      model_atv->appendRow(ItemRow);
                      ItemRow.clear();

          }
          next_atv_loc=i;
          if(i>=tcl_usb->q_mtk_atv_tv.size())
              ui->apB_NextPage->setEnabled(false);
          else
              ui->apB_NextPage->setEnabled(true);

      }else if(tcl_usb->q_mstar_atv_tv.size()!=0)
      {

         Solution="MSTAR-ATV";
        TCL_MSTAR_ATV_TV *mstar_atv_tv;
        count_row_atv=0;
        int i=0;
        int tmp=cur_atv_loc;
        if(last_atv_page)
        {
           last_atv_loc.push_back(tmp);
           last_atv_page=false;
        }

            int j=0;
         for(i=cur_atv_loc;i<tcl_usb->q_mstar_atv_tv.size()&&count_row_atv<SHOW_NUM_DATA;i++)
             {
             j=ms_tv->mstar_atv->ui_ATVPRIndexTable[i];
             if(i>=200||j>=200)
               break;
             if(ms_tv->mstar_atv->ATVProgramData[j].misc.Unused==1
                              &&ms_tv->mstar_atv->ATVProgramData[j].misc.u_Hide==0
                     &&ms_tv->mstar_atv->ATVProgramData[j].ui2_wPLL>0
                )
                {
                      if(j<tcl_usb->q_mstar_atv_tv.size())
                      mstar_atv_tv=tcl_usb->q_mstar_atv_tv.at(j);
                      else
                          break;
                      ItemRow=Get_Item_Row(mstar_atv_tv);
                      count_row_atv++;
                      model_atv->appendRow(ItemRow);
                      ItemRow.clear();
                      mstar_atv_tv=NULL;
                  }
          }
         next_atv_loc=j;
         if(i>=Chan_atv_Num->size())
             ui->apB_NextPage->setEnabled(false);
         else
             ui->apB_NextPage->setEnabled(true);

      }else{
          qDebug("In Show ATV error\n");
      }

      ui->tableView_atv->setModel(model_atv);

      for(int i=0;i<count_row_atv;i++)
      {
          for(int j=0;j<count_col_atv;j++)
          {
               model_atv->item(i,j)->setTextAlignment(Qt::AlignCenter);
          // if(i%2==0)
            //   model_atv->item(i,j)->setBackground(QBrush(QColor(0,191,255)));
          // else
             model_atv->item(i,j)->setBackground(QBrush(QColor(135,206,235)));
          }
      }
      if(last_atv_loc.size()<2)
        ui->apB_LastPage->setEnabled(false);
      else
         ui->apB_LastPage->setEnabled(true);

      int pages=Chan_atv_Num->size()/SHOW_NUM_DATA+1;
      int cur_page=cur_dtv_loc/SHOW_NUM_DATA+1;
      QString show_page;
      show_page.setNum(cur_page);
      show_page+="/";
      show_page+=QString::number(pages);
      ui->atv_page->setText(show_page);
      ui->atv_page->show();
    //  ui->atv_page->setEnabled(false);

      modify=true;
}

   void MainWindow::Show_DTV()
    {
    ///
       model_dtv->clear();
      SetTableHeader(1);
      count_row_dtv=0;
      QList<QStandardItem*> ItemRow;
      modify=false;
      Q_CHECK_PTR(tcl_usb);
      if(tcl_usb->q_mtk_dtv_tv.size()!=0)
      {

          TCL_MTK_DTV_TV *mtk_dtv_tv;
          count_row_dtv=0;
          int i=0;
          int tmp=cur_dtv_loc;
          if(last_dtv_page)
          {
             last_dtv_loc.push_back(tmp);
             last_dtv_page=false;
          }
          for(i=cur_dtv_loc;i<tcl_usb->q_mtk_dtv_tv.size()&&count_row_dtv<SHOW_NUM_DATA;i++)
          {
              if(1==(mt_tv->mtk_dtv.at(i)->ui4_nw_flag&4)
                      ||mt_tv->mtk_dtv.at(i)->ui4_ch_val>9999)
                  continue;
              mtk_dtv_tv=tcl_usb->q_mtk_dtv_tv.at(i);
              Q_CHECK_PTR(mtk_dtv_tv);
              ItemRow=Get_Item_Row(mtk_dtv_tv);
              count_row_dtv++;
              model_dtv->appendRow(ItemRow);
              ItemRow.clear();

          }
          next_dtv_loc=i;

          if(i>=tcl_usb->q_mtk_dtv_tv.size())
              ui->dpB_NextPage->setEnabled(false);
          else
              ui->dpB_NextPage->setEnabled(true);
      }else if(tcl_usb->q_mstar_dtv_tv.size()!=0)
      {

          TCL_MSTAR_DTV_TV *mstar_dtv_tv;
          count_row_dtv=0;
          int i=0;
          int tmp=cur_dtv_loc;
          if(last_dtv_page)
          {
             last_dtv_loc.push_back(tmp);
             last_dtv_page=false;
          }
          for(i=cur_dtv_loc;i<tcl_usb->q_mstar_dtv_tv.size()&&count_row_dtv<SHOW_NUM_DATA;i++)
          {
              if(ms_tv->mstar_dtv.at(i)->u_IsDelete==0&&
                      ms_tv->mstar_dtv.at(i)->ui2_Number<=9999&&
                      ms_tv->mstar_dtv.at(i)->u_ServiceType<5)
                {
                  mstar_dtv_tv=tcl_usb->q_mstar_dtv_tv.at(i);
                  Q_CHECK_PTR(mstar_dtv_tv);

                  count_row_dtv++;
                  ItemRow=Get_Item_Row(mstar_dtv_tv);

                  model_dtv->appendRow(ItemRow);
                  ItemRow.clear();
              }
          }
          next_dtv_loc=i;
          if(i>=ms_tv->Get_DTV_Count())
              ui->dpB_NextPage->setEnabled(false);
          else
              ui->dpB_NextPage->setEnabled(true);

      }else
      {
          qDebug("In Show DTV solution error");
          return ;
      }
      //cur_dtv_loc+=count_col_dtv;
      ui->tableView_dtv->setModel(model_dtv);
      for(int i=0;i<count_row_dtv;i++)
          for(int j=0;j<count_col_dtv;j++)
          {
            model_dtv->item(i,j)->setTextAlignment(Qt::AlignCenter);
          // if(i%2==0)
          // model_dtv->item(i,j)->setBackground(QBrush(QColor(0,191,255)));
          // else
             model_dtv->item(i,j)->setBackground(QBrush(QColor(135,206,235)));
          }
      int pages=(Chan_dtv_air_Num->size()+Chan_dtv_cable_Num->size())/SHOW_NUM_DATA+1;
      int cur_page=cur_dtv_loc/SHOW_NUM_DATA+1;
      QString show_page;
      show_page.setNum(cur_page);
      show_page+="/";
      show_page+=QString::number(pages);
      ui->dtv_page->setText(show_page);
      ui->dtv_page->show();

     // ui->an_save_DTV->setVisible(true);
      if(last_dtv_loc.size()<2)
          ui->dpB_LastPage->setEnabled(false);
      else
          ui->dpB_LastPage->setEnabled(true);
      modify=true;
    }

   void MainWindow::Del_Row()
    {
      int list_row=0;

      if(0==cur_show||2==cur_show)
      {
          is_atv_edit=true;
        //  cur_row=ui->tableView_atv->currentIndex().row();
      }
      else if(1==cur_show||3==cur_show)
      {
          is_dtv_edit=true;
        //  cur_row=ui->tableView_dtv->currentIndex().row();
      }
      else
          qDebug("In MainWindow::Del_Row() cur_show<0 or cur_show >3");


      list_row=Find_List_Row();

      if(2==cur_show)
      {
          if(cur_row<model_atv->rowCount())
          {
            Chan_atv_Num->removeAt(cur_row);
            model_atv->removeRow(cur_row);
            for(int i=0;i<model_atv->rowCount();i++)
            {
               // QModelIndex index(i,0);
                QVariant var=i+1;
                model_atv->setData(model_atv->index(i,0),var);
            }
          }
          else
              return ;
      }else if(0==cur_show)
      {
          if(cur_row<model_atv->rowCount())
          {
              Chan_atv_Num->removeAt(cur_row);
              model_atv->removeRow(cur_row);
          }else
              return ;
          
      }
      else if(3==cur_show)
      {
          if(cur_row<model_dtv->rowCount())
          {
              if(model_dtv->item(cur_row,2)->data(0).toString().compare("DTV-B"))
                  Chan_dtv_air_Num->remove((Chan_dtv_air_Num->begin()+cur_row).key());
              else
                  Chan_dtv_cable_Num->remove((Chan_dtv_cable_Num->begin()+cur_row).key());
            model_dtv->removeRow(cur_row);
          }
          else
              return;
      }else if(1==cur_show)
      {
          if(cur_show<model_dtv->rowCount())
          {
              Chan_dtv_air_Num->remove((Chan_dtv_air_Num->begin()+cur_row).key());
              model_dtv->removeRow(cur_row);
          }
      }else
      {
          qDebug("In MainWindow::Del_Row() cur_show error");
      }

      //According to cur_show value:0-mtk_atv,1-mtk_dtv,2-mstar_atv,3-mstar_dtv
      Q_ASSERT(tcl_usb!=NULL);
    switch(cur_show)
    {
    case 0:
        {
        if(list_row<tcl_usb->q_mtk_atv_tv.size()&&list_row>=0)
          {
            mt_tv->Del_ATV_Row(list_row);
            tcl_usb->Del_ATV_Row(list_row,0);
          }

          break;
        }
    case 1:
        {
        if(list_row<tcl_usb->q_mtk_dtv_tv.size()&&list_row>=0)
        {
            tcl_usb->Del_DTV_Row(list_row,0);
             mt_tv->Del_DTV_Row(list_row);
        }
          break;
         }
    case 2:
        {
        if(list_row<tcl_usb->q_mstar_atv_tv.size()&&list_row>=0)
        {
            ms_tv->Del_ATV_Row(list_row);
            for(ushort i=0;i<200;i++)
            {
                ushort j=ms_tv->Get_ATV_Num(i);
                if(0==j)
                    break;
                QVariant tmp=j;
                tcl_usb->q_mstar_atv_tv.at(i)->Set_Data(0,tmp);
            }
           // Chan_atv_Num->removeLast();

            tcl_usb->Del_ATV_Row(list_row,1);

        }
          break;
          }
    case 3:
        {
        if(list_row<tcl_usb->q_mstar_dtv_tv.size()&&list_row>=0)
        {
            ushort tv_class=tcl_usb->q_mstar_dtv_tv.at(list_row)->Get_Item(2).toUShort();
            //tcl_usb->Del_DTV_Row(list_row,1);
            ms_tv->Del_DTV_Row(list_row,tv_class);//删去一行，同时更新dtvb或dtvbc
        }
          break;
          }

    default:
        qDebug("In DelRow cur_show error");

    }
    }

    void MainWindow::set_cur_row(QModelIndex index)
    {
        cur_row=index.row()>0?index.row():0;
    }

    void MainWindow::set_cur_show(int currentIndex)
    {
        Q_CHECK_PTR(tcl_usb);
        if(0==currentIndex&&0==tcl_usb->Get_Solution())
            cur_show=0;
        else if(0==currentIndex&&1==tcl_usb->Get_Solution())
            cur_show=2;
        else if(1==currentIndex&&0==tcl_usb->Get_Solution())
            cur_show=1;
        else if(1==currentIndex&&1==tcl_usb->Get_Solution())
            cur_show=3;
        else
            qDebug("In MainWindow::set_cur_show cur_show error");
    }

   void MainWindow::Sea_Row()
    {
        Search_Show=new Search();
        Search_Show->show();
        connect(Search_Show,SIGNAL(EndInput(QString )),this,SLOT(Find(QString )));
    }

   void MainWindow::Find(QString input)
    {
        switch(cur_show)
        {
            case 2:
            {
              model_atv->clear();
                for(int i=0;i<tcl_usb->q_mtk_atv_tv.size();i++)
                {

                    TCL_MSTAR_ATV_TV *ms_atv=tcl_usb->q_mstar_atv_tv.at(i);
                    if(!ms_atv)
                        return ;
                    for(int j=0;j<NUM_TV_ITEM;j++)
                    {
                        QString tmp=ms_atv->Get_Item(j);
                        if(tmp.compare(input,Qt::CaseInsensitive)==0)
                            {
                              SetTableHeader(0);
                             QList <QStandardItem *> ItemRow=Get_Item_Row(ms_atv);
                              model_atv->appendRow(ItemRow);
                             break;
                            }

                    }
                }
                break;
            }
            case 3:
            {
            model_dtv->clear();
            for(int i=0;i<tcl_usb->q_mstar_dtv_tv.size();i++)
            {

                if(ms_tv->mstar_dtv.at(i)->u_IsDelete==1)
                    continue;
                TCL_MSTAR_DTV_TV *ms_dtv=tcl_usb->q_mstar_dtv_tv.at(i);
                if(!ms_dtv)
                   continue ;
                for(int j=0;j<NUM_DTV_ITEM;j++)
                {
                    QString tmp=ms_dtv->Get_Item(j);
                    if(tmp.compare(input,Qt::CaseInsensitive)==0)
                   {
                        SetTableHeader(1);
                        QList <QStandardItem *> ItemRow=Get_Item_Row(ms_dtv);
                        model_dtv->appendRow(ItemRow);
                        ui->tableView_dtv->setModel(model_dtv);
                        break;
                    }
                }
             }
            break;
            }
            case 0:
            {
                model_atv->clear();
                for(int i=0;i<tcl_usb->q_mtk_atv_tv.size();i++)
                {
                    TCL_MTK_ATV_TV *mt_atv=tcl_usb->q_mtk_atv_tv.at(i);
                    if(!mt_atv)
                        continue;
                    for(int j=0;j<NUM_TV_ITEM;j++)
                    {
                        QString tmp=mt_atv->Get_Item(j);
                        if(tmp.compare(input,Qt::CaseInsensitive)==0)
                        {
                            SetTableHeader(0);
                            QList <QStandardItem *> ItemRow=Get_Item_Row(mt_atv);
                            model_atv->appendRow(ItemRow);
                            ui->tableView_atv->setModel(model_atv);
                            break;
                        }
                    }

                }
                break;
            }
            case 1:
            {
                model_dtv->clear();
                for(int i=0;i<tcl_usb->q_mtk_dtv_tv.size();i++)
                {
                    TCL_MTK_DTV_TV *mt_dtv=tcl_usb->q_mtk_dtv_tv.at(i);
                    if(!mt_dtv)
                        continue;
                    for(int j=0;j<NUM_DTV_ITEM;j++)
                    {
                        QString tmp=mt_dtv->Get_Item(j);
                        if(0==tmp.compare(input,Qt::CaseInsensitive))
                        {
                            SetTableHeader(1);
                            QList<QStandardItem *> ItemRow=Get_Item_Row(mt_dtv);
                            model_dtv->appendRow(ItemRow);
                            ui->tableView_dtv->setModel(model_dtv);
                            break;
                        }
                    }
                }
                break;
            }
        default:
            break;

        }
    }

   void MainWindow::Show_Menu(QPoint)
    {
      if(cmenu)
      {
         cmenu->clear();
         // cmenu=NULL;
      }
      if(cur_show==0||cur_show==2)
      {
          cmenu=new QMenu(ui->tableView_atv);
      }
      else
      {
          cmenu=new QMenu(ui->tableView_dtv);
      }


      QAction *an_modify=cmenu->addAction("Modify");
      QAction *an_delrow=cmenu->addAction("Delete");
      QAction *an_search=cmenu->addAction("Search");
      QAction *an_Show=cmenu->addAction("Show");
      QAction *an_Add=cmenu->addAction("Add");

      connect(an_modify,SIGNAL(triggered()),this,SLOT(Md_Data()));
      connect(an_delrow,SIGNAL(triggered()),this,SLOT(Del_Row()));
      connect(an_search,SIGNAL(triggered()),this,SLOT(Sea_Row()));
      if(0==cur_show||2==cur_show)
      {
      connect(an_Show,SIGNAL(triggered()),this,SLOT(Show_ATV()));
      connect(an_Add,SIGNAL(triggered()),this,SLOT(Add_ATV_CH()));
      }
      else
      {
          connect(an_Show,SIGNAL(triggered()),this,SLOT(Show_DTV()));
          connect(an_Add,SIGNAL(triggered()),this,SLOT(Add_DTV_CH()));
      }
      cmenu->exec(QCursor::pos());

    }

//row is the data at table location.
   void MainWindow::Md_Data()
    {

        int list_row=-1;
        switch(cur_show)
        {
        case 2:
        {
            list_row=Find_List_Row();
           // md_list_row=list_row;
            int row;
            row=ms_tv->mstar_atv->ui_ATVPRIndexTable[list_row];
            if(row>=tcl_usb->q_mstar_atv_tv.size()&&row<0)
                return;
            Q_ASSERT(tcl_usb!=NULL);
            Q_ASSERT(tcl_usb->q_mstar_atv_tv.size()>=row);
            TCL_MSTAR_ATV_TV *mstar_atv=tcl_usb->q_mstar_atv_tv.at(row);
            mstar_atv->ui2_ch_val=list_row+1;
            Q_CHECK_PTR(mstar_atv);
            if(Chan_atv_Num&&Chan_atv_Num->size()>0)
            {
                if(Chan_dtv_air_Num->size()>0||Chan_dtv_cable_Num->size())
                    md_ms_atv=new Md_atv(mstar_atv,Chan_atv_Num,Chan_dtv_air_Num,Chan_dtv_cable_Num);
                else
                  md_ms_atv=new Md_atv(mstar_atv,Chan_atv_Num);
                Q_CHECK_PTR(md_ms_atv);
                md_ms_atv->show();
                connect(md_ms_atv,SIGNAL(Mstar_Close(TCL_MSTAR_ATV_TV,QList<uint>)),this,SLOT(Do_Md(TCL_MSTAR_ATV_TV,QList<uint>)));
            }
            else
            {
                QMessageBox::information(this,"error","The Mstar dtv Channel Number Qlist pointer is NULL or its size below zero",QMessageBox::Ok);
                return ;
            }
            break;
        }
        case 3:
        {
            list_row=Find_List_Row();
            //md_list_row=
            //qDebug("%d",list_row);
            if(list_row>=tcl_usb->q_mstar_dtv_tv.size()&&list_row<0)
                return;
            Q_ASSERT(tcl_usb->q_mstar_dtv_tv.size()>=list_row);
            TCL_MSTAR_DTV_TV *mstar_dtv=tcl_usb->q_mstar_dtv_tv.at(list_row);
            Q_CHECK_PTR(mstar_dtv);
            if(Chan_dtv_air_Num||Chan_dtv_cable_Num)
            {
                if(Chan_atv_Num->size()>0)
                    md_ms_dtv=new Md_dtv(mstar_dtv,Chan_atv_Num,Chan_dtv_air_Num,Chan_dtv_cable_Num);
                else
                    md_ms_dtv=new Md_dtv(mstar_dtv,Chan_dtv_air_Num,Chan_dtv_cable_Num);
                Q_CHECK_PTR(md_ms_dtv);
                md_ms_dtv->show();

                connect(md_ms_dtv,SIGNAL(Mstar_Close(TCL_MSTAR_DTV_TV,QList<uint>)),this,SLOT(Do_Md(TCL_MSTAR_DTV_TV,QList<uint>)));
            }
            else
            {
                QMessageBox::warning(this,"error","The Mstar dtv Channel Number pointer is NULL or its size below zero",QMessageBox::Ok);
                return ;
            }
            break;
        }
        case 0:
        {
            list_row=Find_List_Row();
            if(list_row>=tcl_usb->q_mtk_atv_tv.size()&&list_row<0)
                return ;
            Q_ASSERT(tcl_usb->q_mtk_atv_tv.size()>=list_row);
            TCL_MTK_ATV_TV *mtk_atv=tcl_usb->q_mtk_atv_tv.at(list_row);
            Q_CHECK_PTR(mtk_atv);
            if(Chan_atv_Num&&Chan_atv_Num->size()>0)
             {
                if(Chan_dtv_air_Num->size()>0||Chan_dtv_cable_Num->size()>0)
                    md_mt_atv=new Md_atv(mtk_atv,Chan_atv_Num,Chan_dtv_air_Num,Chan_dtv_cable_Num);
                else
                    md_mt_atv=new Md_atv(mtk_atv,Chan_atv_Num);
                 Q_CHECK_PTR(md_mt_atv);
                md_mt_atv->show();
                connect(md_mt_atv,SIGNAL(Mtk_Close(TCL_MTK_ATV_TV,QList<uint>)),this,SLOT(Do_Md(TCL_MTK_ATV_TV,QList<uint>)));
            }else
            {
                QMessageBox::information(this,"error","The MTK atv Channel Number pointer is NULL or ites size below zero",QMessageBox::Ok);
                return ;
            }
            break;
        }
        case 1:
        {
            list_row=Find_List_Row();
            if(list_row>=tcl_usb->q_mtk_dtv_tv.size()&&list_row<0)
                return;
            Q_ASSERT(tcl_usb->q_mtk_dtv_tv.size()>=list_row);
            TCL_MTK_DTV_TV *mtk_dtv=tcl_usb->q_mtk_dtv_tv.at(list_row);
            Q_CHECK_PTR(mtk_dtv);
            if(Chan_dtv_air_Num->size()>0||Chan_dtv_cable_Num->size())
            {
                if(Chan_atv_Num->size()>0)
                    md_mt_dtv=new Md_dtv(mtk_dtv,Chan_atv_Num,Chan_dtv_air_Num,Chan_dtv_cable_Num);
                else
                    md_mt_dtv=new Md_dtv(mtk_dtv,Chan_dtv_air_Num,Chan_dtv_cable_Num);
                Q_CHECK_PTR(md_mt_dtv);
                md_mt_dtv->show();
                connect(md_mt_dtv,SIGNAL(Mtk_Close(TCL_MTK_DTV_TV,QList<uint>)),this,SLOT(Do_Md(TCL_MTK_DTV_TV,QList<uint>)));
            }else
            {
                QMessageBox::information(this,"error","The MTK dtv Channel Number pointer is NULL or ites size below zero",QMessageBox::Ok);
                return ;
            }
            break;
        }
        default:
            Q_ASSERT_X(cur_show>=0&&cur_show<4,"In MainWindow::Md_Data() Line 907","cur_show error");
            //qDebug("In MainWindow::Md_Data() cur_show error!");
         }

}
   void MainWindow::Do_Md(TCL_MSTAR_ATV_TV ms_atv,QList<uint> md_item)
    {
        int row=Find_List_Row();
        int qlist_row=ms_tv->mstar_atv->ui_ATVPRIndexTable[row];
        Q_CHECK_PTR(model_atv);
        model_atv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&ms_atv);
        model_atv->insertRow(cur_row,ItemRow);
        for(int i=0;i<ms_atv.Count_Item();i++)
        model_atv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

       // tcl_usb->q_mstar_atv_tv.removeAt(qlist_row);
       // tcl_usb->q_mstar_atv_tv.insert(qlist_row,&ms_atv);
        Q_CHECK_PTR(ms_tv);
        for(int count=0;count<md_item.size();count++)
        {
            int i=md_item.at(count);
            ms_tv->Mod_ATV(qlist_row,i,ms_atv.Get_Item(i));
        }

    }
   void MainWindow::Do_Md(TCL_MSTAR_DTV_TV ms_dtv,QList<uint> md_item)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_dtv);
        model_dtv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&ms_dtv);
        model_dtv->insertRow(cur_row,ItemRow);
        for(int i=0;i<ms_dtv.Count_Item();i++)
        model_dtv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

       // tcl_usb->q_mstar_atv_tv.removeAt(qlist_row);
       // tcl_usb->q_mstar_atv_tv.insert(qlist_row,&ms_atv);
        Q_CHECK_PTR(ms_tv);
        for(int count=0;count<md_item.size();count++)
        {
            int i=md_item.at(count);
            ms_tv->Mod_DTV(qlist_row,i,ms_dtv.Get_Item(i));
        }
    }

   void MainWindow::Do_Md(TCL_MTK_ATV_TV mt_atv,QList<uint> md_item)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_atv);
        model_atv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&mt_atv);
        model_atv->insertRow(cur_row,ItemRow);
        for(int i=0;i<mt_atv.Count_Item();i++)
            model_atv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

        Q_CHECK_PTR(mt_tv);
        for(int count=0;count<md_item.size();count++)
        {
            int i=md_item.at(count);
            mt_tv->Mod_ATV(qlist_row,i,mt_atv.Get_Item(i));
        }


    }

   void MainWindow::Do_Md(TCL_MTK_DTV_TV mt_dtv,QList<uint>md_item)
    {
        int qlist_row=Find_List_Row();
        Q_CHECK_PTR(model_dtv);
        model_dtv->removeRow(cur_row);
        QList<QStandardItem *> ItemRow=Get_Item_Row(&mt_dtv);
        model_dtv->insertRow(cur_row,ItemRow);
        for(int i=0;i<mt_dtv.Count_Item();i++)
            model_dtv->item(cur_row,i)->setTextAlignment(Qt::AlignCenter);

        Q_CHECK_PTR(mt_tv);
        for(int i=0;i<md_item.size();i++)
            for(int j=i+1;j<md_item.size();)
            {
                if(md_item.at(i)==md_item.at(j))
                {
                    md_item.removeAt(j);
                }else
                    j++;
            }
        for(int count=0;count<md_item.size();count++)
        {
            int i=md_item.at(count);
            mt_tv->Mod_DTV(qlist_row,i,mt_dtv.Get_Item(i));
        }

    }

   void MainWindow::Add_ATV_CH()
    {
        uint Max_Ch_Val=Chan_atv_Num->size();

        if(0==solution)
        {
            add_atv=new Add_ATV(solution,Max_Ch_Val+1,Chan_atv_Num,Chan_dtv_air_Num);
            add_atv->show();
            connect(add_atv,SIGNAL(Add_MTK_ATV(TCL_MTK_ATV_TV*)),this,SLOT(Add_MTK_ATV(TCL_MTK_ATV_TV*)));

        }else
        {
            add_atv=new Add_ATV(solution,Max_Ch_Val+1,NULL,NULL );
            add_atv->show();
            connect(add_atv,SIGNAL(Add_MSTAR_ATV(TCL_MSTAR_ATV_TV*)),this,SLOT(Add_MSTAR_ATV(TCL_MSTAR_ATV_TV*)));

        }
   }

   void MainWindow::Add_DTV_CH()
    {
        add_dtv=new Add_DTV(solution,Chan_atv_Num,Chan_dtv_air_Num,Chan_dtv_cable_Num);
        add_dtv->show();
        if(0==solution)
        {
            connect(add_dtv,SIGNAL(Add_MTK_DTV(TCL_MTK_DTV_TV*)),this,SLOT(Add_MTK_DTV(TCL_MTK_DTV_TV*)));
        }else
            connect(add_dtv,SIGNAL(Add_MSTAR_DTV(TCL_MSTAR_DTV_TV*)),this,SLOT(Add_MSTAR_DTV(TCL_MSTAR_DTV_TV*)));
        //Show_DTV();
   }

  void MainWindow::Add_MTK_DTV(TCL_MTK_DTV_TV *mtk_dtv)
    {
        //tcl_usb->q_mtk_dtv_tv.append(mtk_dtv);
        tcl_usb->Add_DTV_CH(mtk_dtv);
        model_dtv->appendRow(Get_Item_Row(mtk_dtv));
        for(int i=0;i<model_dtv->columnCount();i++)
        {
            model_dtv->item(model_dtv->rowCount()-1,i)->setTextAlignment(Qt::AlignCenter);
        }
        QList<QString> tmp;
        mtk_dtv->Get_Data(&tmp);
        mt_tv->Add_DTV_CH(tmp);
     }

  void MainWindow::Add_MSTAR_DTV(TCL_MSTAR_DTV_TV *mstar_dtv)
    {

        //tcl_usb->q_mstar_dtv_tv.append(mstar_dtv);
        tcl_usb->Add_DTV_CH(mstar_dtv);//添加排序
        model_dtv->appendRow(Get_Item_Row(mstar_dtv));
        for(int i=0;i<model_dtv->columnCount();i++)
        {
            model_dtv->item(model_dtv->rowCount()-1,i)->setTextAlignment(Qt::AlignCenter);
        }
        QList<QString> tmp;
        mstar_dtv->Get_Data(&tmp);
        ms_tv->Add_DTV_CH(tmp);
    }

 void MainWindow::Add_MTK_ATV(TCL_MTK_ATV_TV * mtk_atv)
   {
       Chan_atv_Num->append(mtk_atv->ui4_ch_val);//dtv在add——xxx类里
       tcl_usb->q_mtk_atv_tv.append(mtk_atv);
       model_atv->appendRow(Get_Item_Row(mtk_atv));
       for(int i=0;i<model_atv->columnCount();i++)
       {
           model_atv->item(model_atv->rowCount()-1,i)->setTextAlignment(Qt::AlignCenter);
       }

       QList<QString> tmp;
       mtk_atv->Get_Data(&tmp);

       mt_tv->Add_ATV_CH(tmp);
   }

   void MainWindow::Add_MSTAR_ATV(TCL_MSTAR_ATV_TV * mstar_atv)
   {
       //tcl_usb->q_mstar_atv_tv.append(mstar_atv);
       Chan_atv_Num->append(Chan_atv_Num->size()+1);
       model_atv->appendRow(Get_Item_Row(mstar_atv));
       for(int i=0;i<model_atv->columnCount();i++)
       {
           model_atv->item(model_atv->rowCount()-1,i)->setTextAlignment(Qt::AlignCenter);
       }
       int index=0;
       int loc=0;
       for(int i=0;i<200;i++)
       {
           int j=ms_tv->mstar_atv->ui_ATVPRIndexTable[i];
           if(ms_tv->mstar_atv->ATVProgramData[j].misc.Unused==0)
           {
               index=i;
               loc=j;
               break;
           }
       }
       tcl_usb->q_mstar_atv_tv.removeAt(loc);
       tcl_usb->q_mstar_atv_tv.insert(loc,mstar_atv);
       QList<QString> tmp;
       mstar_atv->Get_Data(&tmp);
       ms_tv->Add_ATV_CH(tmp);
   }

   void MainWindow::NextPage()
 {
     if(0==cur_show||2==cur_show)
     {
          last_atv_page=true;
         cur_atv_loc=next_atv_loc;
         model_atv->clear();
         Show_ATV();
     }else if(1==cur_show||3==cur_show)
     {
         last_dtv_page=true;
         cur_dtv_loc=next_dtv_loc;
         model_dtv->clear();
         Show_DTV();
     }else
         qDebug("In MainWindow::NextPage() error");
 }

 void MainWindow::LastPage()
 {
     if(0==cur_show||2==cur_show)
     {
         last_atv_page=true;
         last_atv_loc.pop_back();
         cur_atv_loc=last_atv_loc.takeLast();
         model_atv->clear();
         Show_ATV();
     }else if(1==cur_show||3==cur_show)
     {
         last_dtv_page=true;
         last_dtv_loc.pop_back();
         cur_dtv_loc=last_dtv_loc.takeLast();
         model_dtv->clear();
         Show_DTV();
     }else
         qDebug("In MainWindow::LastPage() error");
 }

 void MainWindow::Show_Ch_Help()
   {
       chinese=new Help(0);
       chinese->show();
   }
 void MainWindow::Show_En_Help()
 {
     english=new Help(1);
     english->show();
 }


    int MainWindow::Check_MS_TV(MSTAR_TV *ms_tv)
    {
       if(!ms_tv->Is_Empty())
        {
           ms_tv->Clear_Data();
        }
        return 1;
    }

    int  MainWindow::Check_MT_TV(MTK_TV *mt_tv)
    {
        if(!mt_tv->Is_Empty())
        {
            mt_tv->Clear_Data();
        }
        return 1;
    }

    void MainWindow::Set_MS_ATV_Num()
    {
        for(int i=0;i<200;i++)
        {
            int j=ms_tv->mstar_atv->ui_ATVPRIndexTable[i];
            if(j>=200||i>=200)
                break;
            if(ms_tv->mstar_atv->ATVProgramData[j].misc.Unused==1)
            {
                QVariant tmp=i+1;
                tcl_usb->q_mstar_atv_tv.at(j)->Set_Data(0,tmp);
            }
        }
    }

    int MainWindow::Con_Mstar_Read(MSTAR_TV* ms_tv,const char *disk,int *solution)
    {

        if(-1==Check_MS_TV(ms_tv))
            return -1;
        int reatv=ms_tv->Read_ATV(disk);
        int redtv=ms_tv->Read_DTV(disk);
      if(-1==reatv&&-1==redtv)
      {
             return -1;
      }else if(-1!=reatv||-1!=redtv)
          *solution=1;
      ms_tv->Empty_Add_One();
      return 1;
    }

    int MainWindow::Con_Mtk_Read(MTK_TV *mt_tv,const char *disk,int *solution)
    {
        if(-1==Check_MT_TV(mt_tv))
                    return -1;
        int revalue=mt_tv->Read_File(disk);
        *solution=0;
        if(1!=revalue)
        {
            return -1;
        }
        mt_tv->Empty_Add_One();
        return 1;
    }
    //file->standard bin
  void MainWindow::Con_std_file()
{
  if(model_atv)
  model_atv->clear();
  if(model_dtv)
  model_dtv->clear();
  if(!Chan_atv_Num->isEmpty())
  {
      Chan_atv_Num->clear();
  }

  if(!Chan_dtv_air_Num->isEmpty())
    Chan_dtv_air_Num->clear();


  if(!Chan_dtv_cable_Num->isEmpty())
      Chan_dtv_cable_Num->clear();

  translate tran;


//for convert files more than one times,I need clear datas last time.
  //if MSTAR
/*
  detect=Detect_Usb(disk);
  if(1==detect)
  {
      if(-1==Con_Mstar_Read(ms_tv,disk,&solution))
      {
          READ_TV_FILE_ERROR;
          return;
      }
  }else if(2==detect)
  {
     if(-1==Con_Mtk_Read(mt_tv,disk,&solution))
     {
         READ_TV_FILE_ERROR;
         return;
     }
  }
  else //mtk and mstar all exist or all not exist
  {
      path=QFileDialog::getExistingDirectory(this,"Please Choose the File Directory","",QFileDialog::ShowDirsOnly);
      if(path.isEmpty())
          return;
      QString tmp1="database";
      QString tmp2="mtk";
      if(path.contains(tmp1))
      {
          solution=1;
          path.remove(path.length()-tmp1.length(),tmp1.length());
      }else if(path.contains(tmp2))
      {
          solution=0;
          path.remove(path.length()-tmp2.length(),tmp2.length());
      }else{
          QMessageBox::warning(this,"Warning","The folder is invalid ,which you chose");
      }
      strcpy(disk,path.toLatin1().data());
      qDebug("%s",disk);
      if(0==solution)
      {
          if(-1==Con_Mtk_Read(mt_tv,disk,&solution))
          {
              READ_TV_FILE_ERROR;
              return ;
          }
      }
      else if(1==solution)
      {
          if(-1==Con_Mstar_Read(ms_tv,disk,&solution))
          {
              READ_TV_FILE_ERROR;
              return ;
          }
      }
      else
      {
          qDebug("other sloution ");
          return;
      }
    }*/

  int convert=0;
  if(0==solution)
  {
     mt_tv->Get_Chan_Num(Chan_atv_Num,Chan_dtv_air_Num);
     convert=tran.convert_bin(mt_tv);
  }
  else if(1==solution)
  {
       ms_tv->Get_Chan_Num(Chan_atv_Num,Chan_dtv_air_Num,Chan_dtv_cable_Num);
       convert=tran.convert_bin(ms_tv);
  }

  if(-1==convert)
  {
      CONVERT_TO_FILE_FAILED;
      return ;
  }

  open_file();
  CONVERT_TO_FILE_SUCCESS ;

  ui->actionOpen_standard_file->setEnabled(true);
  ui->anDel_Row->setEnabled(true);
  ui->actionSearch->setEnabled(true);
  ui->actionShow->setEnabled(true);
  ui->actionModify_Ctrl_M->setEnabled(true);

}

 int Con_Mstar_Write(MSTAR_TV *ms_tv,char *disk)
 {     Q_CHECK_PTR(ms_tv);
     int reatv=ms_tv->Write_ATV(disk);
     int redtv=ms_tv->Write_DTV(disk);
    if(-1==reatv&&-1==redtv)
    {
        return -1;
    }
    return 1;
 }
 int Con_Mtk_Write(MTK_TV *mt_tv, char *disk)
 {
     Q_CHECK_PTR(mt_tv);
     int revalue=mt_tv->Write_File(disk);
     if(1!=revalue)
     {
         return -1;
     }
     return 1;
 }
 void MainWindow::Menu_Open()
{
     detect=Detect_Usb(disk);
     if(-1!=detect)
     {
         ui->an_open_USB->setEnabled(true);
     }else{
         ui->an_open_USB->setEnabled(false);
     }

}

 void MainWindow::Open_USB()
 {
     if(1==detect)
     {
         if(-1==Con_Mstar_Read(ms_tv,disk,&solution))
         {
             READ_TV_FILE_ERROR;
             return;
         }
     }else if(2==detect)
     {
         if(-1==Con_Mtk_Read(mt_tv,disk,&solution))
         {
             READ_TV_FILE_ERROR;
             return;
         }
     }else{
         qDebug("error");
         return ;
     }
     Con_std_file();
 }

 void MainWindow::Open_PC()
 {
     path=QFileDialog::getExistingDirectory(this,"Please Choose the File Directory","",QFileDialog::ShowDirsOnly);
     if(path.isEmpty())
         return;
     QString tmp1="database";
     QString tmp2="mtk";
     if(path.contains(tmp1))
     {
         solution=1;
         path.remove(path.length()-tmp1.length(),tmp1.length());
     }else if(path.contains(tmp2))
     {
         solution=0;
         path.remove(path.length()-tmp2.length(),tmp2.length());
     }else{
         QMessageBox::warning(this,"Warning","The folder is invalid ,which you chose");
     }
     strcpy(disk,path.toLatin1().data());
     qDebug("%s",disk);
     if(0==solution)
     {
         if(-1==Con_Mtk_Read(mt_tv,disk,&solution))
         {
             READ_TV_FILE_ERROR;
             return ;
         }
     }
     else if(1==solution)
     {
         if(-1==Con_Mstar_Read(ms_tv,disk,&solution))
         {
             READ_TV_FILE_ERROR;
             return ;
         }
     }
     else
     {
         qDebug("other sloution ");
         return;
     }
     Con_std_file();
 }

 //data->file,just write data,edited,to file.
 void MainWindow::Con_tv_file()
{
  translate tran;

  Q_CHECK_PTR(mt_tv);
  char disk[FILE_PATH_LEN];
  memset(disk,0,FILE_PATH_LEN);
  if(solution==1)
  {
      ms_tv->Check_ATV();
  }
  int detect=Detect_Usb(disk);
  if(-1==detect)
  {
      strcpy(disk,path.toLatin1().data());
  }

  if(1==solution)
    {
      if(-1==Con_Mstar_Write(ms_tv,disk))
      {
          WRITE_TV_FILE_ERROR;
          return;
      }
  }else if(0==solution){
      if(-1==Con_Mtk_Write(mt_tv,disk))
      {
          WRITE_TV_FILE_ERROR;
          return;
      }
  }
int revalue=tran.bin_modify(disk,solution);
    if(-1==revalue)
    {
        CONVERT_TO_TV_FAILED;
        return;
    }
  //ui->actionConvert_standard_file->setEnabled(true);
  ui->actionConvert_tv_file->setEnabled(false);
  CONVERT_TO_TV_SUCCESS;
}

 int MainWindow::Check_Disk(char *disk)
 {
     WCHAR wsz[64];
     swprintf(wsz,L"%s",disk);
     LPCWSTR p=wsz;
     if(GetDriveType(p)==DRIVE_REMOVABLE)
         return 0;
     return -1;
 }

 int Check_Folder(char *Disk)
 {
     QStringList folders;
     const QString disk=Disk;
     QDir dir(disk);
     folders=dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
     QString tmp1="database";
     QString tmp2="mtk";
     int revalue=-1;
     for(int i=0;i<folders.size();i++)
     {
         if(0==folders.at(i).compare(tmp1))
         {
            revalue=1;
         }
         if(0==folders.at(i).compare(tmp2))
         {
             if(1==revalue)
                 revalue=3;
             else
                 revalue=2;

         }
     }
     return revalue;
 }


  int MainWindow::Detect_Usb(char  *Disk)
 {
     Q_CHECK_PTR(Disk);
     if(!Disk)
         return -1;
     char buf[10];
     memset(buf,0,10);
     DWORD lod=GetLogicalDrives();
     if(lod!=0)
     {
         for(int i=0;i<26;i++)
         {
             DWORD dw_Temp=lod&1;
             if(1==dw_Temp)
             {
                 sprintf(buf,"%c",'A'+i);
                 strcat(buf,":\\");
                 if(!Check_Disk(buf))
                 {
                     strncpy(Disk,buf,3);
                     return Check_Folder(Disk);
                 }
             }
             lod=lod>>1;
         }
     }
     return -1;
 }
